<?php
namespace app\index\controller;
use app\index\controller\BaseController;
use app\index\middleware\Auth;
use app\index\model\UserCollectType;
use Curl\Curl;
use think\Config;
use think\Db;
use think\Request;


/**
 * 产业类
 * Class Industry
 * @package app\index\controller
 */
class Industry extends BaseController
{
    /**
     * 产业资讯分类
     */
    public function industryCategory(){
        $reqData['method'] = $this->request->method();
        $reqData['page'] = $this->request->get('page',0);
        $reqData['limit'] = $this->request->get('limit',10);

        // 业务
        $curl_data = $this->getBusiness('Common')->curl_request_data($reqData['method']);
        if ($curl_data['status'] != 1){
            $this->result['status'] = $curl_data['status'];
            $this->result['msg'] = $curl_data['msg'];
            return json_encode($this->result);
        }

        // 数据分页返回
        $offset = $reqData['page']*$reqData['limit'];
        $res = array_slice($curl_data['data']['data']['data'],$offset,$reqData['limit']);
        $curl_data['data']['data']['data'] = $res;

        $this->result['status'] = 1;
        $this->result['data'] = $curl_data['data']['data'];
        return json_encode($this->result);
    }

    /**
     * 产业资讯列表
     * @return false|string
     */
    public function industryNews()
    {
        $reqData['keywords'] = $this->request->get('keywords','');
        $reqData['identification'] = $this->request->get('identification');
        $reqData['city_id'] = $this->request->get('city_id');
        $reqData['chain_id'] = $this->request->get('chain_id');
        $reqData['page'] = $this->request->get('page',0);
        $reqData['limit'] = $this->request->get('limit',10);
        $reqData['method'] = $this->request->method();
        $dispatch = $this->request->dispatch();
        $new_type=Db::connect(Config::get('db_yd'))->table('yd_news_type')->where(['status'=>1,'identification'=>$reqData['identification']])->value('type_name');
        if ($reqData['identification'] !=6){
            // 业务
            $curl_data = $this->getBusiness('Common')->curl_request_data($reqData['method'] ,array('city_id'=>$reqData['city_id'],'chain_id'=>$reqData['chain_id'],'keywords' => $reqData['keywords'],'identification' => $reqData['identification'],'page'=>$reqData['page'],'limit'=>$reqData['limit']));
            if ($curl_data['status'] != 1){
                $this->result['status'] = $curl_data['status'];
                $this->result['msg'] = $curl_data['msg'];
                return json_encode($this->result);
            }
            // 我的收藏字段
            $return_data = $this->getBusiness('Common')->clickCollet($dispatch,$curl_data['data']['data']['data'],Config::get('collect.1'));
            $curl_data['data']['data']['data'] = $return_data;
            foreach ($curl_data['data']['data']['data'] as &$val){
                $val['type_id']=1;
                $val['news_name']='【'.$val['type_name'].'】'.$val['news_name'];
            }
            $this->result['status'] = 1;
            $this->result['msg'] = 'success';
            $this->result['data'] = $curl_data['data']['data'];
            return json_encode($this->result);
        }else{
            Config::set('paginate.page',bcadd($reqData['page'],1));
            $obj=db('publish_news')
                ->field('news_name,identification,desc,image as thumb,content,news_origin as froms,created_at as create_time')
                ->where('status',1);
//                ->order('create_time','desc')
//                ->paginate($reqData['limit'])
//                ->toArray();
            /**模糊查询**/
            if(!empty($reqData['keywords'])){
                $obj->where('news_name','like','%'.$reqData['keywords'].'%');
            }
            $result=$obj->order('grow desc,create_time desc')->paginate($reqData['limit'])->toArray();
            foreach ($result['data'] as &$val){
                $val['type_id']=0;
                $val['news_name']='【'.$new_type.'】'.$val['news_name'];
                $val['url']='';
            }

            $return_data = $this->getBusiness('Common')->clickCollet($dispatch,$result['data'],Config::get('collect.1'));

            $result['data']=$return_data;
            $this->result['status'] = 1;
            $this->result['msg'] = 'success';
            $this->result['data'] = $result;
            return json_encode($this->result);
        }
    }

    /**
     * 产业资讯详情
     * @return false|string
     */
    public function industrydetail(){
        $reqData['identification'] = $this->request->get('identification');
        $reqData['type_id'] = $this->request->get('type_id');
        $reqData['method'] = $this->request->method();
        $dispatch = $this->request->dispatch();

        if (!$reqData['identification']){
            $this->result['status'] = -1;
            $this->result['msg'] = '参数错误';
            return json_encode($this->result);
        }
        // 业务
        if ($reqData['type_id'] !=6){
            $curl_data = $this->getBusiness('Common')->curl_request_data($reqData['method'],['identification' => $reqData['identification'],'index'=>'news']);
            if ($curl_data['status'] != 1){
                $this->result['status'] = $curl_data['status'];
                $this->result['msg'] = $curl_data['msg'];
                return json_encode($this->result);
            }

            // 我的收藏字段
                                                                                                                                                                                                                                                                                                                                                                                               $single = array($curl_data['data']['data']);
            $return_data = $this->getBusiness('Common')->clickCollet($dispatch,$single,Config::get('collect.1'));
            $curl_data['data']['data'] = $return_data;

            $this->result['status'] = 1;
            $this->result['msg'] = 'success';
            $this->result['data'] = $curl_data['data']['data'];
            return json_encode($this->result);
        }else{
            $result=db('publish_news')
                ->field('news_name,identification,desc,image as thumb,content,news_origin as froms,created_at as create_time')
                ->where('status',1)
                ->where('identification',$reqData['identification'])
                ->find();
            $this->result['status'] = 1;
            $this->result['msg'] = 'success';
            $this->result['data'] = $result;
            return json_encode($this->result);
        }

    }
}